package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.service.CheckGroupService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 新增检查组
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Map map){
        Map formData = (Map) map.get("formData");
        List muenss = (List) map.get("muens");
        List permissionss = (List) map.get("permissions");
        Role role = new Role();
        role.setName((String) formData.get("name"));
        role.setKeyword((String) formData.get("keyword"));
        role.setDescription((String) formData.get("description"));
        Integer[] muens = new Integer[muenss.size()];
        for(Integer i=0;i<muenss.size();i++){
            muens[i]=(Integer) muenss.get(i);
        }

        Integer[] permissions = new Integer[permissionss.size()];
        for(Integer i=0;i<permissionss.size();i++){
            permissions[i]=(Integer) permissionss.get(i);
        }



        try {
            roleService.add(muens,role,permissions);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    /**
     * 检查项的分页查询
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 通过id查询检查组
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id){

        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);

        }

    }

    /**
     * 编辑更新检查组
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Map map){
        Map formData = (Map) map.get("formData");
        List muenss = (List) map.get("muens");
        List permissionss = (List) map.get("permissions");
        Role role = new Role();
        role.setId((Integer) formData.get("id"));
        role.setName((String) formData.get("name"));
        role.setKeyword((String) formData.get("keyword"));
        role.setDescription((String) formData.get("description"));
        Integer[] menus = new Integer[muenss.size()];
        for(Integer i=0;i<muenss.size();i++){
            menus[i]=(Integer) muenss.get(i);
        }

        Integer[] permissions = new Integer[permissionss.size()];
        for(Integer i=0;i<permissionss.size();i++){
            permissions[i]=(Integer) permissionss.get(i);
        }

        try {
            roleService.edit(menus,role,permissions);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }

    /**
     * 通过id删除检查组
     */

    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id")Integer id){

        try {
            roleService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    /**
     * 查询所有检查组信息
     */
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Role> roles = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);

        }
    }

    /**
     * 通过检查组id查询检查项id集合
     */
    @RequestMapping("findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam("id")Integer id){

        try {
            List<Integer> checkItemIds = roleService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }



}
